package com.company.nuwa.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @author slj
 */
public class BigDecimalUtil {

    /**
     * 分转换元
     *
     * @param value
     * @return
     */
    public static BigDecimal changeF2Y(BigDecimal value) {
        if (value != null) {
            return value.divide(new BigDecimal(100), 2, RoundingMode.DOWN);
        }
        return null;
    }

    /**
     * 分转换元
     *
     * @param value
     * @return
     */
    public static BigDecimal changeF2Y(Integer value) {
        if (Objects.isNull(value)) {
            return null;
        }
        return BigDecimal.valueOf(value).divide(new BigDecimal(100), 2, RoundingMode.DOWN);
    }

    /**
     * 分转换元
     *
     * @param value
     * @return
     */
    public static BigDecimal changeF2Y(Long value) {
        if (Objects.isNull(value)) {
            return null;
        }
        return BigDecimal.valueOf(value).divide(new BigDecimal(100), 2, RoundingMode.DOWN);
    }

    /**
     * 元转分
     *
     * @param value
     * @return
     */
    public static Integer changeY2IntegerF(BigDecimal value) {
        if (Objects.isNull(value)) {
            return null;
        }
        return value.multiply(new BigDecimal(100)).intValue();
    }

    /**
     * 元转分
     *
     * @param value price
     * @return price
     */
    public static Long changeY2F(BigDecimal value) {
        if (value != null) {
            return value.multiply(new BigDecimal(100)).longValue();
        }
        return null;
    }

    /**
     * 计算折扣
     *
     * @param originPrice 原价
     * @param sellPrice   销售价
     * @return 折扣... 5折...
     */
    public static BigDecimal getDiscount(BigDecimal originPrice, BigDecimal sellPrice) {
        if (originPrice == null || sellPrice == null) {
            return null;
        }
        if (originPrice.compareTo(sellPrice) <= 0) {
            return null;
        }
        return BigDecimal.valueOf(1).subtract(originPrice.subtract(sellPrice).divide(originPrice, 1, RoundingMode.DOWN)).multiply(BigDecimal.TEN);
    }

    /**
     * 两个数字之间的比率
     *
     * @param first
     * @param second
     * @return
     */
    public static BigDecimal ratioCalculation(BigDecimal first, BigDecimal second) {
        if (Objects.isNull(first) || Objects.isNull(second)) {
            return BigDecimal.ZERO;
        }
        if (first.compareTo(BigDecimal.ZERO) == 0 || second.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }
        return first.divide(second, 4, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));
    }

    /**
     * 两个数字之间的比率
     *
     * @param first
     * @param second
     * @return
     */
    public static BigDecimal ratioCalculation(Long first, Long second) {
        if (Objects.isNull(first) || Objects.isNull(second)) {
            return BigDecimal.ZERO;
        }
        return ratioCalculation(BigDecimal.valueOf(first), BigDecimal.valueOf(second));
    }

}
